#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cmath>
#include<vector>
#include<limits.h>
using namespace std;
void get_oppro(vector<int>& v, int x)
{
	for (int i = 2; i <= sqrt(x); ++i)
	{
		if (x % i == 0)
		{
			v.push_back(i);
			if (x / i != i) v.push_back(x / i);
		}
	}
}
int get_my_path(int n, int m)
{
	vector<int> path(m + 1, INT_MAX);
	path[n] = 0;
	for (int i = n; i < m; ++i)
	{
		if (path[i] == INT_MAX)
			continue;
		vector<int> v;
		get_oppro(v, i);
		for (int j = 0; j < v.size(); ++j)
		{
			if (i + v[j] <= m && path[v[j] + i] != INT_MAX)
				path[v[j] + i] = min(path[v[j] + i], path[i] + 1);
			else if (i + v[j] <= m && path[v[j] + i] == INT_MAX)
				path[v[j] + i] = path[i] + 1;
		}
	}
	return path[m] == INT_MAX ? -1 : path[m];
}
int main()
{
	int n, m;
	while (cin >> n >> m)
	{
		int res = get_my_path(n, m);
		cout << res << endl;
	}
	return 0;
}